/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package	dao;

import Hibernate.HibernateUtil;
import pojo.Author;
import pojo.Translator;


import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class TranslatorDAO extends AbstractDAO {

	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return Translator.class;
	}
	
    public Translator getTranslatorByID(int id){
        Translator translator = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            translator = (Translator)session.get(Translator.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return translator;
    }
    public int insert(String translatorName){
    	Translator translator = new Translator();
    	translator.setName(translatorName);
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(translator);
			transaction.commit();
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
		}
		finally {
			session.close();
			return translator.getTranslatorId();
		}
    }
    public static synchronized Translator checkAndInsert(String translatorName){
    	//boolean isFind = false;
    	//int translatorID = -1;
    	TranslatorDAO translatordao = new TranslatorDAO();
    	List<Translator> translators = translatordao.getList();
    	if(translators.isEmpty()){
    		Translator translator = new Translator();
    		translator.setName(translatorName);
    		Session session = HibernateUtil.getSessionFactory().openSession();
    		Transaction transaction = null;
    		try {
    			transaction = session.beginTransaction();
    			session.save(translator);
    			transaction.commit();
    		} catch (HibernateException ex) {
    			// Log the exception
    			transaction.rollback();
    		}
    		finally {
    			session.close();
    			//translatorID = translator.getTranslatorId();
    			return translator;
    		}
    	}
    	else{
    		int size = translators.size();
    		for(int i = 0; i < size; i++){
    			if(java.text.Normalizer.normalize(translatorName, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","").compareTo(java.text.Normalizer.normalize(translators.get(i).getName(), java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","")) == 0){
    				//translatorID = translators.get(i).getTranslatorId();
    				//isFind = true;
    				//break;
    				return translators.get(i);
    			}
    		}
    		//if(!isFind){
    		Translator translator = new Translator();
        	translator.setName(translatorName);
       		Session session = HibernateUtil.getSessionFactory().openSession();
       		Transaction transaction = null;
       		try {
       			transaction = session.beginTransaction();
       			session.save(translator);
       			transaction.commit();
       		} catch (HibernateException ex) {
       			// Log the exception
        			transaction.rollback();
       		}
       		finally {
       			session.close();
       			//translatorID = translator.getTranslatorId();
       			return translator;
       		}
    		//}
    	}
    	//return translatorID;
    }
    
    public List<Author> getTopTranslator(int n)
    {
    	List<Author> ds= null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = "select s from Translator s join s.books b group by s.translatorId order by count(b) desc " ;
		Query query = session.createQuery(hql);
		
		query.setMaxResults(n);
		ds =  query.list();
		session.close();
    	return ds;
    }
}
